package cn;/*
 * Copyright (c) 2006, 2021, webrx.cn All rights reserved.
 *
 */

import cn.webrx.mapper.StudentMapper;
import cn.webrx.mapper.UserMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.datasource.pooled.PooledDataSource;
import org.apache.ibatis.mapping.Environment;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.ibatis.transaction.TransactionFactory;
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;

import javax.sql.DataSource;

/**
 * <p>Project: mybatis2021 - cn.MyDemo
 * <p>Powered by webrx On 2021-07-12 14:44:04
 * <p>Created by IntelliJ IDEA
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 15
 */
@Slf4j
public class MyDemo {
    public static void main(String[] args) {
        //建立数据源，
        PooledDataSource ds = new PooledDataSource();
        ds.setDriver("com.mysql.cj.jdbc.Driver");
        ds.setUrl("jdbc:mysql://localhost:3306/wxdb");
        ds.setUsername("root");
        ds.setPassword("nieps");

        //建立事务管理对象
        TransactionFactory tx = new JdbcTransactionFactory();

        //建立mybatis环境
        Environment env = new Environment("development", tx, ds);
        //建立配置对象，相当于加个mybatis-config.xml
        Configuration cfg = new Configuration(env);

        //cfg.addMapper(UserMapper.class);
        cfg.addMappers("cn.webrx.mapper");

        SqlSessionFactory sf = new SqlSessionFactoryBuilder().build(cfg);
        SqlSession ss = sf.openSession();
        UserMapper udao = ss.getMapper(UserMapper.class);

        log.info(udao.ver().toString());


        log.info(udao.queryAll().toString());

        StudentMapper sdao = ss.getMapper(StudentMapper.class);
        log.debug(sdao.dbs().toString());
    }
}
